import json
import hashlib
import time

from selenium import webdriver

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

options = Options()
# 禁止图片和css加载
prefs = {"profile.managed_default_content_settings.images": 2, 'permissions.default.stylesheet': 2}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options)


def get_name_content(i):
    """
    获取name和content
    :param i:
    :return:
    """

    try:
        op1 = i.find_element(By.XPATH,
                             './/div/div/article/div/div/div[2]'
                             '/div[2]/div[1]/div[1]/div[1]/div'
                             '/div/div[2]/div/div[1]/a/div/span')
        if op1 is None:
            return None, None
        name = op1.text
        op2 = i.find_element(By.XPATH,
                             './/div/div/article/div/div/div[2]/div[2]/div[2]/div/span')
        if op2 is None:
            return None, None
        content = op2.text
        return name, content
    except Exception as e:
        return None, None


def add_all(data: dict, aim: list, only):
    """
    添加列表内容到字典中去
    :param only:
    :param data:
    :param aim:
    :return:
    """
    for i in aim:
        name, content = get_name_content(i)
        if name is None or content is None:
            continue
        if name != only:
            continue
        id = hashlib.md5(content.encode("utf-8")).hexdigest()
        if data.get(id) is None:
            one = {'@name': name,
                   'content': content}
            data[id] = one


def save(obj, path):
    """
    将对象存入json文件
    :param obj:
    :return:
    """
    with open(path, 'w', encoding="UTF-8") as f:
        json.dump(obj, f, ensure_ascii=False)


def load_cookies():
    """
    加载cookie
    :param args:
    :return:
    """
    with open('./cookies.json', 'r', encoding='UTF-8') as f:
        cookies = json.load(f)
    return cookies


def get_and_save_cookies():
    """
    获取浏览器当前的cookie
    :return:
    """
    save(driver.get_cookies(), './cookies.json')


def open_chrome(cookies, url):
    """
    打开网页
    :param cookies:
    :return:
    """
    driver.get(url)
    for cookie in cookies:
        driver.add_cookie(cookie)
    driver.get(url)
    driver.implicitly_wait(1)
    driver.maximize_window()


def control_Chrome():
    """
    控制浏览器
    :return:
    """
    only_name = driver.find_element(By.XPATH,
                                    '/html/body/div[1]/div/div/div[2]'
                                    '/main/div/div/div/div[1]/div/div[3]'
                                    '/div/div/div/div/div[2]/div/div/div[2]'
                                    '/div/div/div/span').text
    data = {}
    while len(data) <= 30:
        driver.execute_script("document.documentElement.scrollTop += 1200")
        els = driver.find_elements(By.XPATH,
                                   '//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[1]/div/div['
                                   '3]/div/div/section/div/div/div/div/div')
        add_all(data, els, only_name)
        print(len(data))
    return data


def close_chrome():
    driver.close()


if __name__ == '__main__':
    try:
        url = 'https://twitter.com/POTUS'
        file_name = url.split("com/")[1]
        start = time.time()
        cookies = load_cookies()
        open_chrome(cookies, 'https://twitter.com/POTUS')
        data = control_Chrome()
        save(data, './' + file_name + '.json')
        print(time.time() - start)
    finally:
        close_chrome()
